//
//  SmallDecisionsDlpViewController.m
//  SmallDecisions
//
//  Created by 李付雄 on 2025/7/25.
//

#import "SmallDecisionsDlpViewController.h"
#import "SmallDecisionsDlpComplateViewController.h"
#import "SmallDecisionsDlpSetItemsViewController.h"
#import "SmallDecisionsPlateView.h"

@interface SmallDecisionsDlpViewController ()<SmallDecisionsPlateViewDelegate>
@property (nonatomic, strong) SmallDecisionsPlateView *plateView;

@end

@implementation SmallDecisionsDlpViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
    NSString *userPath = [documentPath stringByAppendingPathComponent:@"LuckyDisc.plist"];
    if ([fileManager fileExistsAtPath:userPath]) {
        NSArray *data = [NSArray arrayWithContentsOfFile:userPath];
        self.plateView.data = data;
        [self.plateView startPlateDraw];
    } else {
        self.plateView.data = @[@"你最近一次“我爱你”是对谁说的？",
                                @"你在感情上劈过腿没有?",
                                @"你和对象做过最刺激的事情是什么?",
                                @"你亲吻过多少人?",
                                @"走错过男女厕所么?",
                                @"你是抖S或者抖M吗?",
                                @"到目前为止写过多少封情书?"];
        [self.plateView startPlateDraw];
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"幸运转盘";
    [self plateView];
    [self.view layoutIfNeeded];
}

- (SmallDecisionsPlateView *)plateView {
    if(!_plateView) {
        _plateView = [[SmallDecisionsPlateView alloc] init];
        _plateView.delegate = self;
        [self.view addSubview:_plateView];
        [_plateView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(15);
            make.right.mas_equalTo(-15);
            make.centerY.mas_equalTo(self.view);
            make.height.mas_equalTo(_plateView.mas_width);
        }];
    }
    
    return _plateView;
}

- (void)plateViewDidFinish:(SmallDecisionsPlateView *)plateView target:(NSInteger)target {
    NSString *complateContent = plateView.data[target];
    SmallDecisionsDlpComplateViewController * complateVC = [[SmallDecisionsDlpComplateViewController alloc] init];
    complateVC.complateContent = complateContent;
    [SmallDecisionsNavigatorManager presentViewController:complateVC showNavigationBar:NO animationType:SmallDecisionsAnimationTypeAlert];
}



- (UIView *)setRightBarButton {
    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [rightBtn setTitle:@"自定义玩法" forState:UIControlStateNormal];
    rightBtn.titleLabel.font = SmallDecisionsSetBoldFontSize(14);
    [rightBtn setTitleColor:SmallDecisionsThemeFontColor forState:UIControlStateNormal];
    rightBtn.frame = CGRectMake(0, 0, 30, 30);
    return rightBtn;
}

- (void)rightBarButtonAction:(UIButton *)view {
    SmallDecisionsDlpSetItemsViewController *setItemsVC = [[SmallDecisionsDlpSetItemsViewController alloc] init];
    setItemsVC.list = [self.plateView.data copy];
    setItemsVC.callback = ^(NSArray *messages) {
        if (messages.count > 0) {
            NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
            NSString *messagesPath = [documentPath stringByAppendingPathComponent:@"LuckyDisc.plist"];
            NSData *messagesData = [messages plistData];
            [messagesData writeToFile:messagesPath atomically:YES];
            self.plateView.data = messages.copy;
            [self.plateView startPlateDraw];
        }
    };
    [SmallDecisionsNavigatorManager sysPresentViewController:setItemsVC];
}

@end
